package collection;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.TimeUnit;

public class collection5 {

    public static void main(String[] args) throws InterruptedException {
        Run5 run5 = new Run5(new Obj5());
//        new Thread(run5).start();
//        TimeUnit.SECONDS.sleep(1);
//        new Thread(run5).start();

        for (int i = 0; i < 30; i++) {
            new Thread(run5,"Thread"+(i+1)).start();
        }
    }
}



class Run5 implements Runnable{
    private Obj5 obj;

    public Run5(){

    }

    public Run5(Obj5 obj) {
        this.obj = obj;
    }

    Integer count = 1;

    @Override
    public void run() {
        SetAndReadObj();

    }

    public synchronized void SetAndReadObj(){
        String s = "name" + count++;
        obj.setName(s);
        System.out.println(Thread.currentThread().getName()+" set obj:"+s);

        System.out.println(Thread.currentThread().getName()+" read obj:"+obj.getName());
    }
}

@Data
class Obj5{
    private String name;


}